package com.apes.scm.stock.transfer.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.frame.model.DutyCenter;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：耗材领用单
 *
 * @author zhuyh
 * @time 10:48
 * @date 2019/11/20
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ConsumableUse.ConsumableUse_Name)
public class ConsumableUse extends Model {

    public static final String ConsumableUse_Name = "stock_consumableuse";


    @Id
    @Column(name = "id")
    @Comment("耗材领用单号")
    @ApesGenerator(code = ConsumableUse_Name, name = "耗材领用单", prefix = "HL%(company)%(yy)%(month)", padding = 5, useDateRange=true)
    private String id;

    @Comment("原单类型")
    @Column(name = "srcType", length = 32)
    private String srcType;

    @Comment("来源单据名称")
    @Column(name = "srcName", length = 200)
    private String srcName;

    @Comment("来源单据编码")
    @Column(name = "srcId", length = 32)
    private String srcId;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "location_id", referencedColumnName = "id")
    @Comment("库存位置")
    private Location location;

    @Column(name = "origin", length = 200)
    @Comment("源单据")
    private String origin; //源单据

    @NotNull
    @Comment("部门")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    private Dept dept;

    @Comment("业务员")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "clerk_id", referencedColumnName = "id")
    private PartyRole clerk;

    @States(name = "订单状态", values = {
            @State(id = "sent", name = "创建"),
            @State(id = "confirmed", name = "审核"),
            @State(id = "done", name = "已完成"),
            @State(id = "cancel", name = "已取消"),
            @State(id = "delete", name = "删除")})
    @Column(name = "state", length = 10, nullable = false)
    @Comment("订单状态")
    private String state = "sent";


    @Column(name = "note", length = 120)
    @Comment("备注")
    private String note; //备注

    @Comment("创建日期")
    @Column(name = "date_create")
    private Date dateCreate;

    @Comment("审批日期")
    @Column(name = "date_approve")
    private Date dateApprove;

    @Comment("完成日期")
    @Column(name = "date_done")
    private Date dateDone;

    @NotNull
    @Comment("公司")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "move_type", referencedColumnName = "id")
    @Comment("交货方式")
    private Enumeration moveType; //交货方式：("direct", "部分交货"), ("one", "一次性交货");

//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "cost_center_id", referencedColumnName = "id")
//    @Comment("成本中心")
//    private DutyCenter costCenterId;  //成本中心



    @OneToMany(mappedBy = "consumableUse", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<ConsumableUseItem> consumableUseItems = new HashSet();

    public Set<ConsumableUseItem> getConsumableUseItems() {
        for (ConsumableUseItem item : consumableUseItems) {
            // 筛选数量为零的行
            if (item.getUseQty() == 0) {
                consumableUseItems.remove(item);
                continue;
            }
            // 设置主表以及主表公司
            item.setConsumableUse(this);
            item.setCompany(this.company);
        }
        return consumableUseItems;
    }

}
